//
//  UIImageView+Download.m
//  BuDeJie
//
//  Created by haoran on 16/4/16.
//  Copyright © 2016年 xuhaoran. All rights reserved.
//

#import "UIImageView+Download.h"
#import <AFNetworking/AFNetworking.h>

@implementation UIImageView (Download)


/**
 sd_setImageWithURL:placeholderImage:方法的执行步骤
 1.取消当前imageView之前关联的请求
 2.设置占位图片到当前imageView上面
 3.如果缓存中有对应的图片，那么就显示到当前imageView上面
 4.如果缓存中没有对应的图片，发送请求给服务器下载图片
 */
-(void)xhr_setImageWithOriginalImageURL:(NSString *)originalImageURL thumbnailImageURL:(NSString *)thumbnailImageURL placeholderImage:(UIImage *)placeholderImage completed:(SDWebImageCompletionBlock)completionBlock{
    
    // 从内存\沙盒缓存中获得原图
    UIImage * originalImage = [[SDImageCache sharedImageCache]imageFromDiskCacheForKey:originalImageURL];
    
    if (originalImage) { // 如果内存\沙盒缓存有原图，那么就直接显示原图（不管现在是什么网络状态）
        [self sd_setImageWithURL:[NSURL URLWithString:originalImageURL] completed:completionBlock];
    }else { // 内存\沙盒缓存没有原图
        AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
        if (manager.isReachableViaWiFi) {// 如果是WiFi环境 下载原图
            [self sd_setImageWithURL:[NSURL URLWithString:originalImageURL] placeholderImage:placeholderImage completed:completionBlock];
        }else if(manager.isReachableViaWWAN){// 如果是蜂窝数据
            //     用户的配置项假设利用NSUserDefaults存储到了沙盒中
            //    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"alwaysDownloadOriginalImage"];
            //    [[NSUserDefaults standardUserDefaults] synchronize];
            BOOL alwaysDownloadOriginalImage = [[NSUserDefaults standardUserDefaults]boolForKey:@"alwaysDownloadOriginalImage"];
#warning 从沙盒中读取用户的配置项：在蜂窝网络环境是否仍然下载原图
            if (alwaysDownloadOriginalImage) { // 下载原图
                [self sd_setImageWithURL:[NSURL URLWithString:originalImageURL] placeholderImage:placeholderImage completed:completionBlock];
            }else{ // 下载小图
                [self sd_setImageWithURL:[NSURL URLWithString:thumbnailImageURL] placeholderImage:placeholderImage completed:completionBlock];
            }
        }else{ // 没有网络 访问沙盒有没有小图缓存
            UIImage *thumbnailImage = [[SDImageCache sharedImageCache]imageFromDiskCacheForKey:thumbnailImageURL];
            if (thumbnailImage) { // 内存\沙盒缓存中有小图
                [self sd_setImageWithURL:[NSURL URLWithString:thumbnailImageURL] placeholderImage:placeholderImage completed:completionBlock];
            }else{
                [self sd_setImageWithURL:nil placeholderImage:placeholderImage completed:completionBlock];
            }
            
        }
    }
}

-(void)xhr_setImageWithOriginalImageURL:(NSString *)originalImageURL thumbnailImageURL:(NSString *)thumbnailImageURL completed:(SDWebImageCompletionBlock)completionBlock{

    [self xhr_setImageWithOriginalImageURL:originalImageURL thumbnailImageURL:thumbnailImageURL placeholderImage:nil completed:completionBlock];
}



-(void)xhr_setImageWithOriginalImageURL:(NSString *)originalImageURL thumbnailImageURL:(NSString *)thumbnailImageURL placeholderImage:(UIImage *)placeholderImage{
    
    [self xhr_setImageWithOriginalImageURL:originalImageURL thumbnailImageURL:thumbnailImageURL placeholderImage:placeholderImage completed:nil];

}

-(void)xhr_setImageWithOriginalImageURL:(NSString *)originalImageURL thumbnailImageURL:(NSString *)thumbnailImageURL {
    
    [self xhr_setImageWithOriginalImageURL:originalImageURL thumbnailImageURL:thumbnailImageURL placeholderImage:nil completed:nil];
    
}


@end
